package com.shu.course

import io.reactivex.rxjava3.core.Observable
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

data class User(
    val avatar_url: String,
    val bio: Any?,
    val blog: Any?,
    val created_at: String,
    val events_url: String,
    val followers: Int,
    val followers_url: String,
    val following: Int,
    val following_url: String,
    val gists_url: String,
    val html_url: String,
    val id: Int,
    val login: String,
    val name: String,
    val organizations_url: String,
    val public_gists: Int,
    val public_repos: Int,
    val received_events_url: String,
    val remark: String,
    val repos_url: String,
    val stared: Int,
    val starred_url: String,
    val subscriptions_url: String,
    val type: String,
    val updated_at: String,
    val url: String,
    val watched: Int,
    val weibo: Any?
)

enum class Order {
    Desc("desc"), Asc("asc");

    private val order: String

    constructor(order: String) {
        this.order = order
    }

    override fun toString(): String {
        return this.order
    }
}

interface GiteeApi {
//    Rxjava写法
//    @GET("search/users")
//    fun search(
//        @Query("q") query: String,
//        @Query("page") page: Int,
//        @Query("per_page") perPage: Int,
//        @Query("order") order: Order = Order.Asc
//    ): Observable<List<User>>

//    协程写法
//    @GET("search/users")
//    suspend fun search(
//        @Query("q") query: String,
//        @Query("page") page: Int,
//        @Query("per_page") perPage: Int,
//        @Query("order") order: Order = Order.Asc
//    ): List<User>

//  纯Okhttp写法
    @GET("search/users")
    fun search(
        @Query("q") query: String,
        @Query("page") page: Int,
        @Query("per_page") perPage: Int,
        @Query("order") order: Order = Order.Asc
    ): Call<List<User>>
    companion object {
        private const val BASE_URL = "https://gitee.com/api/v5/"
        fun create(): GiteeApi {
            return Retrofit.Builder().baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build()
                .create(GiteeApi::class.java)
        }
    }
}